package mspdt.ontrade;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.modifier.FadeOutModifier;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.batch.SpriteGroup;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.opengl.texture.render.RenderTexture;
import org.andengine.opengl.util.GLState;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.IGameInterface.OnCreateResourcesCallback;
import org.andengine.ui.IGameInterface.OnCreateSceneCallback;
import org.andengine.ui.IGameInterface.OnPopulateSceneCallback;
import org.andengine.ui.activity.LayoutGameActivity;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;
import org.andengine.util.debug.Debug;
import org.andengine.util.modifier.IModifier;
import org.andengine.util.modifier.ease.EaseBackInOut;
import org.andengine.util.modifier.ease.EaseCircularOut;
import org.andengine.util.modifier.ease.EaseLinear;
import org.andengine.util.modifier.ease.EaseSineOut;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Game3Activity extends LayoutGameActivity implements IOnMenuItemClickListener, IOnSceneTouchListener{

	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 720;
	private Camera mCamera;
	private Scene mainScene;
	private Sprite info;
    private boolean mMotionStreaking = false;
    EditText m_players, f_players;
    ArrayList<String> f_names = new ArrayList<String>(), m_names = new ArrayList<String>();
    private Font mFont;
    Button done;
    private Sound wheel, click, gz;
    IEntityModifier spinning;
    Sprite female, male, spin, arrow, back, share;
    private BitmapTextureAtlas  maleTextureAtlas, spinTextureAtlas, frameTextureAtlas;
    private ITextureRegion  maleTextureRegion, spinTextureRegion, frameTextureRegion;
    VertexBufferObjectManager vertexBufferObjectManager = null;
    LinearLayout ll;
	float f_h = 0, m_h = 0;
	float spin_time_f = 0.0f, spin_time_m = 0.0f;
	TextView tv_m = null, tv_f = null;
	@Override
	public EngineOptions onCreateEngineOptions() {
		// TODO Auto-generated method stub
		mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		
		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(), mCamera);
		engineOptions.getAudioOptions().setNeedsSound(true);
		return engineOptions;
	}

	
	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		// TODO Auto-generated method stub
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("pic/");
		maleTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 300, 300, TextureOptions.DEFAULT);
		frameTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 240, 120, TextureOptions.DEFAULT);
		maleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(maleTextureAtlas, this, "circle.png", 0, 0);
		frameTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(frameTextureAtlas, this, "game3_arrow.png", 0, 0);
		maleTextureAtlas.load();
		frameTextureAtlas.load();
		this.mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, Color.WHITE_ABGR_PACKED_INT);
		this.mFont.load();
		BitmapTextureAtlas backTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 127, 127, TextureOptions.DEFAULT);
		ITextureRegion backTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backTextureAtlas, this, "back.png", 0, 0);
		backTextureAtlas.load();
		back = new Sprite(20, -10, 80, 80, backTextureRegion, mEngine.getVertexBufferObjectManager())
		{
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
					if(TouchEvent.ACTION_UP == pSceneTouchEvent.getAction())
					{
						this.registerEntityModifier(new SequenceEntityModifier(new IEntityModifierListener() {
							@Override
							public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
								Game3Activity.this.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										click.play();
										//mainScene.getBackground().registerBackgroundModifier(pBackgroundModifier)
									}
								});
							}

							@Override
							public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
								Game3Activity.this.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										mainScene.clearTouchAreas();
										mainScene.detachChildren();
										mainScene.clearUpdateHandlers();
										mEngine.stop();
										Game3Activity.this.finish();
									}
								});
							}
						}, new SequenceEntityModifier(new MoveModifier(0.6f, back.getX(), back.getX(), back.getY(), back.getY()-back.getHeight(), EaseBackInOut.getInstance()), new DelayModifier(0))));
					}
				return true;
			}
		};
		
		BitmapTextureAtlas shareTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 127, 127, TextureOptions.DEFAULT);
		ITextureRegion shareTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(shareTextureAtlas, this, "share.png", 0, 0);
		shareTextureAtlas.load();
		share = new Sprite(370, -10, 80, 80, shareTextureRegion, mEngine.getVertexBufferObjectManager())
		{
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
					if(TouchEvent.ACTION_UP == pSceneTouchEvent.getAction())
					{
						this.registerEntityModifier(new SequenceEntityModifier(new IEntityModifierListener() {
							@Override
							public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
								Game3Activity.this.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										click.play();
										//mainScene.getBackground().registerBackgroundModifier(pBackgroundModifier)
									}
								});
							}

							@Override
							public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
								Game3Activity.this.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										showShare();
										//mEngine.stop();
										//Game3Activity.this.finish();
									}
								});
							}
						}, new SequenceEntityModifier(new MoveModifier(0.6f, share.getX(), share.getX(), share.getY(), share.getY()-share.getHeight(), EaseBackInOut.getInstance()), new DelayModifier(0))));
					}
				return true;
			}
		};
		share.setScaleY(0.9f);
		BitmapTextureAtlas infoAtlas = new BitmapTextureAtlas(this.getTextureManager(), 39, 39, TextureOptions.DEFAULT);
		ITextureRegion infoRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(infoAtlas, this, "help.png", 0, 0);
		infoAtlas.load();
		info = new Sprite(430, 670, 40, 40, infoRegion, mEngine.getVertexBufferObjectManager())
		{
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionUp())
				{
					final AlertDialog.Builder alert = new AlertDialog.Builder(Game3Activity.this);
					alert.setTitle(getResources().getString(R.string.info_title));
					alert.setMessage(getResources().getString(R.string.game3_info));
					//alert.show();
					Game3Activity.this.runOnUiThread(new Runnable() {
					    @Override
					    public void run() {
					    	alert.show();
//					    	Game1Activity.this.showDialog(MY_DIALOG_ID);
					    }
					});
				}
				return true;
			}
		};

		BitmapTextureAtlas spinTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 243, 77, TextureOptions.DEFAULT);
		ITextureRegion spinTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(spinTextureAtlas, this, "rerotate.png", 0, 0);
		spinTextureAtlas.load();
		spin = new Sprite(240-120, 680-50, 243, 77, spinTextureRegion, mEngine.getVertexBufferObjectManager())
		{
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if(TouchEvent.ACTION_DOWN==pSceneTouchEvent.getAction())
				{
					this.registerEntityModifier(new SequenceEntityModifier(new ScaleModifier(0.1f, 0.6f, 0.65f), new ScaleModifier(0.1f, 0.65f, 0.6f)));
				}
				else if(TouchEvent.ACTION_UP == pSceneTouchEvent.getAction())
					rotateRing();
				return true;
			}
		};
		spin.setScale(0.6f);
		
		SoundFactory.setAssetBasePath("mfx/");
		try {
			this.click = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "click.mp3");
			this.wheel = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "game3_wheel.mp3");
			this.gz = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "game3_match.mp3");
		} catch (final IOException e) {
			Debug.e(e);
		}
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}
	@Override
	public void onBackPressed()
	{
		back.registerEntityModifier(new SequenceEntityModifier(new IEntityModifierListener() {
			@Override
			public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
				Game3Activity.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						click.play();
					}
				});
			}
			@Override
			public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
				Game3Activity.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mainScene.clearTouchAreas();
						mainScene.detachChildren();
						mainScene.clearUpdateHandlers();
						mEngine.stop();
						Game3Activity.this.finish();
					}
				});
			}
		}, new SequenceEntityModifier(new MoveModifier(0.6f, back.getX(), back.getX(), back.getY(), back.getY()-back.getHeight(), EaseBackInOut.getInstance()), new DelayModifier(0))));
	}
	public void showShare()
	{
		share.registerEntityModifier(new SequenceEntityModifier(new MoveModifier(0.6f, share.getX(), share.getX(), share.getY(), share.getY()+share.getHeight(), EaseBackInOut.getInstance()), new DelayModifier(0)));
	}
	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		// TODO Auto-generated method stub
		mainScene = new Scene();
		BitmapTextureAtlas bgTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 768, 1024, TextureOptions.DEFAULT);
		ITextureRegion bgTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bgTextureAtlas, this, "game3_back.jpg", 0, 0);
		bgTextureAtlas.load();
		Sprite bgSprite = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, bgTextureRegion, mEngine.getVertexBufferObjectManager())
		{
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				//if(devil!=Integer.parseInt(this.getUserData().toString()))
					//this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
				//male.clearEntityModifiers();
				return true;
			}
		};
		SpriteBackground bg = new SpriteBackground(bgSprite);
		mainScene.setBackground(bg);
		mainScene.attachChild(back);
		mainScene.registerTouchArea(back);
		//mainScene.attachChild(share);
		mainScene.registerTouchArea(share);
		mainScene.attachChild(info);
		mainScene.registerTouchArea(info);
		vertexBufferObjectManager = this.getVertexBufferObjectManager();
		
		
		
		//rotateBottle();
		pOnCreateSceneCallback.onCreateSceneFinished(this.mainScene);
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		// TODO Auto-generated method stub
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

	
	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
			return false;

		
	}


	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		// TODO Auto-generated method stub
		//rotateBottle();
		return true;
	}

	@Override
	protected int getLayoutID() {
		// TODO Auto-generated method stub
		return R.layout.game3_layout;
	}

	@Override
	protected int getRenderSurfaceViewID() {
		// TODO Auto-generated method stub
		return R.id.game3_surface;
	}
	public void startGame()
	{
		
	}
	@Override
	protected void onSetContentView() {
		super.onSetContentView();

		this.f_players = (EditText)this.findViewById(R.id.f_players);
		this.m_players = (EditText)this.findViewById(R.id.m_players);
		this.tv_f = (TextView)this.findViewById(R.id.tv_f_names);
		this.tv_m = (TextView)this.findViewById(R.id.tv_m_names);
		this.done = (Button)this.findViewById(R.id.start_game);
		this.done.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//spinning = new LoopEntityModifier(new SequenceEntityModifier(new MoveModifier(0.7f, male.getX(), male.getX(), 0, 360)));
				// TODO Auto-generated method stub
				if(f_names.size()>0 && m_names.size()>0)
				{
					InputMethodManager imm = (InputMethodManager)getSystemService(
						      Game3Activity.this.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(f_players.getWindowToken(), 0);
					float circle_diametr = 400; 
					float radius = circle_diametr/3;
					spin_time_f = 0.0f; spin_time_m = 0.0f;
					arrow = new Sprite(CAMERA_WIDTH/2-50, CAMERA_HEIGHT/2-circle_diametr/3-25+circle_diametr/2, 100, 50, frameTextureRegion, mEngine.getVertexBufferObjectManager())
					{
						@Override
						public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
							//if(devil!=Integer.parseInt(this.getUserData().toString()))
								//this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
							//male.clearEntityModifiers();
							return true;
						}
					};
					male = new Sprite(CAMERA_WIDTH-circle_diametr/2, CAMERA_HEIGHT/2-circle_diametr/3, circle_diametr, circle_diametr, maleTextureRegion, mEngine.getVertexBufferObjectManager())
					{
						@Override
						public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
							
							return true;
						}
					};
					female = new Sprite(-circle_diametr/2, CAMERA_HEIGHT/2-circle_diametr/3, circle_diametr, circle_diametr, maleTextureRegion, mEngine.getVertexBufferObjectManager())
					{
						@Override
						public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
							
							return true;
						}
					};
					float transparent = 0.5f;
					Color line_color = Color.WHITE;
					for(int i=0;i<f_names.size();i++)
					{
						Text f_name = new Text(0, 0, mFont, f_names.get(i), new TextOptions(HorizontalAlign.LEFT), vertexBufferObjectManager);
						float angle = 360/f_names.size()*i;
						f_name.setRotation(angle+270);
						f_name.setScale(0.8f);
						float posX = (float)(radius*Math.cos((-90+angle)*(Math.PI/180)))+female.getWidth()/2-f_name.getWidth()/2;
				    	float poxY = (float)(radius*Math.sin((-90+angle)*(Math.PI/180)))+female.getHeight()/2-f_name.getHeight()/2;
				    	
				    	float lineX1 = female.getWidth()/2;
				    	float lineY1 = female.getHeight()/2;
				    	float lineX2 = (float)(circle_diametr/2*Math.cos((-90+angle+(360/f_names.size())/2)*(Math.PI/180)))+female.getWidth()/2;
				    	float lineY2 = (float)(circle_diametr/2*Math.sin((-90+angle+(360/f_names.size())/2)*(Math.PI/180)))+female.getHeight()/2;
				    	Line line = new Line(lineX1, lineY1, lineX2, lineY2, 3f, vertexBufferObjectManager);
				    	line.setColor(line_color);
				    	line.setAlpha(transparent);
				    	female.attachChild(line);
				    	f_name.setPosition(posX, poxY);
				    	
						female.attachChild(f_name);
					}
					for(int i=0;i<m_names.size();i++)
					{
						Text m_name = new Text(0, 0, mFont, m_names.get(i), new TextOptions(HorizontalAlign.LEFT), vertexBufferObjectManager);
						float angle = 360/m_names.size()*i;
						m_name.setRotation(angle+90);
						m_name.setScale(0.8f);
						float posX = (float)(radius*Math.cos((-90+angle)*(Math.PI/180)))+male.getWidth()/2-m_name.getWidth()/2;
				    	float poxY = (float)(radius*Math.sin((-90+angle)*(Math.PI/180)))+male.getHeight()/2-m_name.getHeight()/2;
				    	
				    	float lineX1 = male.getWidth()/2;
				    	float lineY1 = male.getHeight()/2;
				    	float lineX2 = (float)(circle_diametr/2*Math.cos((-90+angle+(360/m_names.size())/2)*(Math.PI/180)))+male.getWidth()/2;
				    	float lineY2 = (float)(circle_diametr/2*Math.sin((-90+angle+(360/m_names.size())/2)*(Math.PI/180)))+male.getHeight()/2;
				    	Line line = new Line(lineX1, lineY1, lineX2, lineY2, 3f, vertexBufferObjectManager);
				    	line.setColor(line_color);
				    	line.setAlpha(transparent);
				    	male.attachChild(line);
				    	m_name.setPosition(posX, poxY);
						male.attachChild(m_name);
					}
					
					mainScene.attachChild(male);
					mainScene.attachChild(female);
					mainScene.attachChild(arrow);
					mainScene.attachChild(spin);
					mainScene.registerTouchArea(spin);
					rotateRing();
					ll.setVisibility(View.GONE);
				}
				else
				{
					AlertDialog.Builder alert = new AlertDialog.Builder(Game3Activity.this);
					alert.setTitle(getResources().getString(R.string.game3_error_title));
					alert.setMessage(getResources().getString(R.string.game3_error_body));
					alert.show();
				}
				for(String f_n:f_names)
				{
					Log.d("TEST", "f="+f_n);
				}
				
			}
		});
		Values vl = new Values();
		if(vl.getF().size()>0)
		{
			f_names = vl.getF();
			tv_f.setText(f_names.toString()+"("+f_names.size()+")");
		}
		if(vl.getM().size()>0)
		{
			m_names = vl.getM();
			tv_m.setText(m_names.toString()+"("+m_names.size()+")");
		}
			
		this.ll = (LinearLayout)this.findViewById(R.id.game3_ll);
	}
	public void onResume()
	{
		super.onResume();
		if(!mEngine.isRunning())
		mEngine.start();
	}
	public void onPause()
	{
		super.onPause();
		mEngine.stop();
	}
	public void addF(View v)
	{
		if(!f_players.getText().toString().equals(""))
		{
			f_names.add(f_players.getText().toString());
			f_players.setText("");
			tv_f.setText(f_names.toString()+"("+f_names.size()+")");
		}
		else
		{
			final AlertDialog.Builder alert = new AlertDialog.Builder(Game3Activity.this);
			alert.setTitle(getResources().getString(R.string.game2_error_title));
			alert.setMessage(getResources().getString(R.string.game3_error_body));
			//alert.show();
			Game3Activity.this.runOnUiThread(new Runnable() {
			    @Override
			    public void run() {
			    	alert.show();
//			    	Game1Activity.this.showDialog(MY_DIALOG_ID);
			    }
			});
		}
		
		
	}
	public void minusF(View v)
	{
		if(f_names.size()>0)
		{
			if(f_names.size()==1)
			{
				f_names.clear();
				tv_f.setText("");
			}
			else
			{
				f_names.remove(f_names.size()-1);
				tv_f.setText(f_names.toString()+"("+f_names.size()+")");
			}
		}
		
	}
	public void minusM(View v)
	{
		if(m_names.size()>0)
		{
			if(m_names.size()==1)
			{
				m_names.clear();
				tv_m.setText("");
			}
			else
			{
				m_names.remove(m_names.size()-1);
				tv_m.setText(m_names.toString()+"("+m_names.size()+")");
			}
		}
		
	}
	public void addM(View v)
	{
		if(!m_players.getText().toString().equals(""))
		{
			m_names.add(m_players.getText().toString());
			m_players.setText("");
			tv_m.setText("");
			tv_m.setText(m_names.toString()+"("+m_names.size()+")");
		}
		else
		{
			final AlertDialog.Builder alert = new AlertDialog.Builder(Game3Activity.this);
			alert.setTitle(getResources().getString(R.string.game2_error_title));
			alert.setMessage(getResources().getString(R.string.game3_error_body));
			//alert.show();
			Game3Activity.this.runOnUiThread(new Runnable() {
			    @Override
			    public void run() {
			    	alert.show();
//			    	Game1Activity.this.showDialog(MY_DIALOG_ID);
			    }
			});
		}
	}
	public void rotateRing()
	{
		//wheel.stop();
		//wheel.setLooping(true);
		wheel.play();
		male.setRotation(male.getRotation()%360);
		female.setRotation(female.getRotation()%360);
		float m_start = male.getRotation();
		float f_start = female.getRotation();
		arrow.registerEntityModifier(new LoopEntityModifier(new SequenceEntityModifier(new RotationModifier(0.1f, 0f, 5f), new RotationModifier(0.1f, 5f, 0f))));
		Random rand = new Random();
		float end_rot = rand.nextInt(360);
		male.registerEntityModifier(new SequenceEntityModifier(new IEntityModifierListener() {
			@Override
			public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
				Game3Activity.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						
					}
				});
			}

			@Override
			public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
				Game3Activity.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						//male.setRotation(male.getRotation()%360);
						
					}
				});
			}
		}, new SequenceEntityModifier(new RotationModifier(5, m_start, end_rot+3600, EaseCircularOut.getInstance()), new DelayModifier(0))));
		end_rot = rand.nextInt(360);
		female.registerEntityModifier(new SequenceEntityModifier(new IEntityModifierListener() {
			@Override
			public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
				Game3Activity.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						
					}
				});
			}

			@Override
			public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
				Game3Activity.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						//male.setRotation(male.getRotation()%360);
						arrow.clearEntityModifiers();
						arrow.setRotation(0);
						gz.play();
						wheel.stop();
						float sector = 360/f_names.size();
						float current_angle = female.getRotation()%360;
						Log.d("TEST", "sector="+sector);
						Log.d("TEST", "current="+current_angle);
						String w_name = f_names.get((int)((current_angle)/sector));
						Log.d("TEST", "name="+w_name);
					}
				});
			}
		}, new SequenceEntityModifier(new RotationModifier(5, f_start, end_rot+3600, EaseCircularOut.getInstance()), new DelayModifier(0))));
	}
	public void onDestroy()
	{
		super.onDestroy();
		Values vl = new Values();
		vl.setF(f_names);
		vl.setM(m_names);
	}
}